
/*
 * IpdDump_NewGUI.java
 *
 * Created on 10 Ιουν 2009, 11:50:15 πμ
 */
package gui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Collection;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import org.quaternions.ipddump.*;
import org.quaternions.ipddump.data.*;
import org.quaternions.ipddump.writers.*;

/**
 *
 * @author Jimmys Daskalakis
 */
public class IpdDump_NewGUI extends javax.swing.JFrame {

    private String path;
    private String pathString;
    private final String welcome = "Welcome to IPDdump - http://code.google.com/p/ipddump/";
    private String ClipBoardTemp;
    private String ext;
    private String fToSave;
    private InteractivePagerBackup database;
    private TableModel SMSDataModel;
    private final int SMStabINDEX;
    private final int ContactstabINDEX;
    private final int CalendartabINDEX;
    private final int TaskstabINDEX;
    private final int OptionstabINDEX;
    private final int MemostabINDEX;
    final private int SMSWasSentIndex = 0;
    final private int SMSNumberIndex = 1;
    final private int SMSTextIndex = 2;
    final private int SMSSentIndex = 3;
    final private int SMSReceivedIndex = 4;
    private int ActiveTAB;
    private int totalSMS = 0;
    private int[] SMSSelectedRows;
    private int totalContacts = 0;
    private int[] ContactsSelectedRows;
    private final int totalCalendar = 0;
    private int[] CalendarSelectedRows;
    private int totalMemos = 0;
    private int[] MemosSelectedRows;
    private final int totalTasks = 0;
    private int[] TasksSelectedRows;
    private final int totalOptions = 0;
    private int[] OptionsSelectedRows;
    private final DataViewer viewer;
    private final FileWriters fileWriter = new FileWriters();
    private SmsWriters smsWriter;
    private ContactsWriters ContactsWriter;
    private MemosWriters MemosWriter;
    private TableModel ContactsDataModel;
    private TableModel MemosDataModel;
    private final int ContactsNameIndex = 0;
    private final int ContactsEmailIndex = 1;
    private final int ContactsMobileIndex = 2;
    private final int ContactsHomeNumberIndex = 3;
    ContactFinder contactFinder;
    private boolean resolveNames = true;
    private final int MemosTitleIndex = 0;
    private final int MemosMemoIndex = 1;
    private Collection<Contact> xe;
    private Object[] xe2;

    /** Creates new form IpdDump_NewGUI */
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        IpdChooser = new javax.swing.JFileChooser();
        MessageFrame = new javax.swing.JFrame();
        jFileChooser1 = new javax.swing.JFileChooser();
        jPopupMenu = new javax.swing.JPopupMenu();
        jMenuItemTxt = new javax.swing.JMenuItem();
        jMenuItemXML = new javax.swing.JMenuItem();
        jMenuItemCSV = new javax.swing.JMenuItem();
        jMenu = new javax.swing.JMenu();
        jMenuItemCPTXT = new javax.swing.JMenuItem();
        jMenuItemCPXML = new javax.swing.JMenuItem();
        jMenuItemCPCSV = new javax.swing.JMenuItem();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelSMS = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSMS = new javax.swing.JTable();
        jPanelContacts = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableContacts = new javax.swing.JTable();
        jPanelMemo = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableMemos = new javax.swing.JTable();
        jPanelCalendar = new javax.swing.JPanel();
        status_label7 = new javax.swing.JLabel();
        jPanelTasks = new javax.swing.JPanel();
        status_label5 = new javax.swing.JLabel();
        jPanelOptions = new javax.swing.JPanel();
        status_label6 = new javax.swing.JLabel();
        status_label = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        ResolveCheckBox = new javax.swing.JCheckBoxMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        IpdChooser.setAcceptAllFileFilterUsed(false);

        javax.swing.GroupLayout MessageFrameLayout = new javax.swing.GroupLayout(MessageFrame.getContentPane());
        MessageFrame.getContentPane().setLayout(MessageFrameLayout);
        MessageFrameLayout.setHorizontalGroup(
            MessageFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        MessageFrameLayout.setVerticalGroup(
            MessageFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenuItemTxt.setText("View in PlainText");
        jMenuItemTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemTxtActionPerformed(evt);
            }
        });
        jPopupMenu.add(jMenuItemTxt);

        jMenuItemXML.setText("View in Xml");
        jMenuItemXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemXMLActionPerformed(evt);
            }
        });
        jPopupMenu.add(jMenuItemXML);

        jMenuItemCSV.setText("View in Csv");
        jMenuItemCSV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCSVActionPerformed(evt);
            }
        });
        jPopupMenu.add(jMenuItemCSV);

        jMenu.setText("Copy in-->");

        jMenuItemCPTXT.setText("Plain Text");
        jMenuItemCPTXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCPTXTActionPerformed(evt);
            }
        });
        jMenu.add(jMenuItemCPTXT);

        jMenuItemCPXML.setText("XML");
        jMenuItemCPXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCPXMLActionPerformed(evt);
            }
        });
        jMenu.add(jMenuItemCPXML);

        jMenuItemCPCSV.setText("CSV");
        jMenuItemCPCSV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCPCSVActionPerformed(evt);
            }
        });
        jMenu.add(jMenuItemCPCSV);

        jPopupMenu.add(jMenu);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("IPDdump");
        setMinimumSize(new java.awt.Dimension(100, 500));

        jTableSMS.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Sent?", "Number", "Text", "Sent Date", "Received Date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableSMS.setName("SMS"); // NOI18N
        jTableSMS.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSMSMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSMS);
        jTableSMS.getColumnModel().getColumn(0).setMinWidth(40);
        jTableSMS.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTableSMS.getColumnModel().getColumn(0).setMaxWidth(40);
        jTableSMS.getColumnModel().getColumn(0).setHeaderValue("Sent?");
        jTableSMS.getColumnModel().getColumn(1).setMaxWidth(130);
        jTableSMS.getColumnModel().getColumn(1).setHeaderValue("Number");
        jTableSMS.getColumnModel().getColumn(2).setMinWidth(150);
        jTableSMS.getColumnModel().getColumn(2).setHeaderValue("Text");
        jTableSMS.getColumnModel().getColumn(3).setMaxWidth(200);
        jTableSMS.getColumnModel().getColumn(3).setHeaderValue("Sent Date");
        jTableSMS.getColumnModel().getColumn(4).setMaxWidth(200);
        jTableSMS.getColumnModel().getColumn(4).setHeaderValue("Received Date");

        javax.swing.GroupLayout jPanelSMSLayout = new javax.swing.GroupLayout(jPanelSMS);
        jPanelSMS.setLayout(jPanelSMSLayout);
        jPanelSMSLayout.setHorizontalGroup(
            jPanelSMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 749, Short.MAX_VALUE)
        );
        jPanelSMSLayout.setVerticalGroup(
            jPanelSMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("SMS", jPanelSMS);

        jTableContacts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Email", "Mobile", "Home"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableContacts.setName("Contacts"); // NOI18N
        jTableContacts.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableContactsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableContacts);

        javax.swing.GroupLayout jPanelContactsLayout = new javax.swing.GroupLayout(jPanelContacts);
        jPanelContacts.setLayout(jPanelContactsLayout);
        jPanelContactsLayout.setHorizontalGroup(
            jPanelContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 749, Short.MAX_VALUE)
        );
        jPanelContactsLayout.setVerticalGroup(
            jPanelContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Contacts", jPanelContacts);

        jTableMemos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title", "Memo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableMemos.setName("Memos"); // NOI18N
        jTableMemos.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMemosMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTableMemos);
        jTableMemos.getColumnModel().getColumn(0).setPreferredWidth(150);
        jTableMemos.getColumnModel().getColumn(0).setMaxWidth(150);

        javax.swing.GroupLayout jPanelMemoLayout = new javax.swing.GroupLayout(jPanelMemo);
        jPanelMemo.setLayout(jPanelMemoLayout);
        jPanelMemoLayout.setHorizontalGroup(
            jPanelMemoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 749, Short.MAX_VALUE)
        );
        jPanelMemoLayout.setVerticalGroup(
            jPanelMemoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Memos", jPanelMemo);

        status_label7.setFont(new java.awt.Font("Tahoma", 0, 18));
        status_label7.setText("Coming Soon");

        javax.swing.GroupLayout jPanelCalendarLayout = new javax.swing.GroupLayout(jPanelCalendar);
        jPanelCalendar.setLayout(jPanelCalendarLayout);
        jPanelCalendarLayout.setHorizontalGroup(
            jPanelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCalendarLayout.createSequentialGroup()
                .addGap(306, 306, 306)
                .addComponent(status_label7, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addGap(306, 306, 306))
        );
        jPanelCalendarLayout.setVerticalGroup(
            jPanelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCalendarLayout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(status_label7, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addGap(212, 212, 212))
        );

        jTabbedPane1.addTab("Calendar", jPanelCalendar);

        status_label5.setFont(new java.awt.Font("Tahoma", 0, 18));
        status_label5.setText("Coming Soon");

        javax.swing.GroupLayout jPanelTasksLayout = new javax.swing.GroupLayout(jPanelTasks);
        jPanelTasks.setLayout(jPanelTasksLayout);
        jPanelTasksLayout.setHorizontalGroup(
            jPanelTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTasksLayout.createSequentialGroup()
                .addGap(306, 306, 306)
                .addComponent(status_label5, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addGap(306, 306, 306))
        );
        jPanelTasksLayout.setVerticalGroup(
            jPanelTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTasksLayout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(status_label5, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addGap(212, 212, 212))
        );

        jTabbedPane1.addTab("Tasks", jPanelTasks);

        status_label6.setFont(new java.awt.Font("Tahoma", 0, 18));
        status_label6.setText("Coming Soon");

        javax.swing.GroupLayout jPanelOptionsLayout = new javax.swing.GroupLayout(jPanelOptions);
        jPanelOptions.setLayout(jPanelOptionsLayout);
        jPanelOptionsLayout.setHorizontalGroup(
            jPanelOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOptionsLayout.createSequentialGroup()
                .addGap(306, 306, 306)
                .addComponent(status_label6, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addGap(306, 306, 306))
        );
        jPanelOptionsLayout.setVerticalGroup(
            jPanelOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOptionsLayout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(status_label6, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addGap(212, 212, 212))
        );

        jTabbedPane1.addTab("Options", jPanelOptions);

        status_label.setFont(new java.awt.Font("Tahoma", 0, 12));
        status_label.setText("Welcome to IPDdump - http://code.google.com/p/ipddump/");

        fileMenu.setText("File");

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setText("Open");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        ResolveCheckBox.setSelected(true);
        ResolveCheckBox.setText("Resolve Names?");
        ResolveCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResolveCheckBoxActionPerformed(evt);
            }
        });
        fileMenu.add(ResolveCheckBox);

        saveAsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveAsMenuItem.setText("Save Selected As ...");
        saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        /*
         * TODO Commented out until we put in dialog boxes to
         * do something.
         */
//        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(status_label, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 754, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(status_label)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("tabbedPane");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-762)/2, (screenSize.height-560)/2, 762, 560);
    }// </editor-fold>//GEN-END:initComponents

    public IpdDump_NewGUI() {
        initComponents();
        viewer = new DataViewer();
        IpdChooser.setAcceptAllFileFilterUsed(false);
        IpdChooser.setFileHidingEnabled(false);
        IpdChooser.addChoosableFileFilter(
                new ExtensionFileFilter(
                new String[]{".IPD"}, "BB Backup Files"));

        jFileChooser1.setAcceptAllFileFilterUsed(false);
        jFileChooser1.setFileHidingEnabled(false);
        jFileChooser1.addChoosableFileFilter(
                new ExtensionFileFilter(
                new String[]{".csv"}, "Cvs Document *.csv"));
        jFileChooser1.addChoosableFileFilter(
                new ExtensionFileFilter(
                new String[]{".xml"}, "Xml Document *.xml"));
        jFileChooser1.addChoosableFileFilter(
                new ExtensionFileFilter(
                new String[]{".txt"}, "Text Document *.txt"));

        SMSDataModel = jTableSMS.getModel();
        ContactsDataModel = jTableContacts.getModel();
        MemosDataModel = jTableMemos.getModel();
        SMStabINDEX = jTabbedPane1.indexOfTab("SMS");
        ContactstabINDEX = jTabbedPane1.indexOfTab("Contacts");
        CalendartabINDEX = jTabbedPane1.indexOfTab("Calendar");
        TaskstabINDEX = jTabbedPane1.indexOfTab("Tasks");
        OptionstabINDEX = jTabbedPane1.indexOfTab("Options");
        MemostabINDEX = jTabbedPane1.indexOfTab("Memos");
        ActiveTAB = jTabbedPane1.getSelectedIndex();

        status_label.setText(welcome);
        saveAsMenuItem.setEnabled(false);
    }
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        //jTextArea1.setText(thnx+"\n\nWORKING...  be patient!");
        IpdChooser.setMultiSelectionEnabled(false);
        if (JFileChooser.APPROVE_OPTION == IpdChooser.showOpenDialog(null)) {


            path = IpdChooser.getSelectedFile().getAbsolutePath().toString();
            int last = path.lastIndexOf('.');
            pathString = path.substring(0, last);
            jFileChooser1.setSelectedFile(IpdChooser.getSelectedFile());


            String[] args = {IpdChooser.getSelectedFile().getPath()};
            try {
                database = new IPDParser(args[0]).parse();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(MessageFrame, "ERROR: " + ex.getMessage());
                saveAsMenuItem.setEnabled(false);
            }
            resolveNames = ResolveCheckBox.isSelected();

            smsWriter = new SmsWriters(database, resolveNames);
            totalSMS = smsWriter.getNumberOfSMS();

            ContactsWriter = new ContactsWriters(database);
            totalContacts = ContactsWriter.getNumberOfContacts();
            contactFinder = new ContactFinder(database);

            MemosWriter = new MemosWriters(database);
            totalMemos = MemosWriter.getNumberOfMemos();



            if (database != null) {
                saveAsMenuItem.setEnabled(true);
            //ResolveCheckBox.setEnabled(true);
            } else {
                saveAsMenuItem.setEnabled(false);
            //ResolveCheckBox.setEnabled(false);
            }

            fillTables();
        }

    }//GEN-LAST:event_openMenuItemActionPerformed
    private void fillTables() {
        resolveNames = ResolveCheckBox.isSelected();

        fillSMSTable();
        fillContactsTable();
        fillMemosTable();
    }

    private void fillSMSTable() {

        smsTablePrepair();
        jTabbedPane1.setTitleAt(SMStabINDEX, "SMS (" + totalSMS + ")");
        int i = 0;
        String sSent = "";
        String Name = "";

        for (SMSMessage record : database.smsRecords()) {
            if (record.wasSent()) {
                sSent = "true";
            } else {
                sSent = "false";
            }
            if (resolveNames) {
                Name = contactFinder.findContactByPhoneNumber(record.getNumber());
            } else {
                Name = record.getNumber();
            }
            SMSDataModel.setValueAt(sSent, i, SMSWasSentIndex);
            SMSDataModel.setValueAt(Name, i, SMSNumberIndex);
            SMSDataModel.setValueAt(record.getText(), i, SMSTextIndex);
            SMSDataModel.setValueAt(record.getSent().toString(), i, SMSSentIndex);
            SMSDataModel.setValueAt(record.getReceived().toString(), i, SMSReceivedIndex);
            i++;//Go to next Line in the table
        }
    }

    private void fillContactsTable() {

        ContactsTablePrepair();
        jTabbedPane1.setTitleAt(ContactstabINDEX, "Contacts (" + totalContacts + ")");
        int i = 0;

        for (Contact record : database.contacts()) {

            ContactsDataModel.setValueAt(record.getName(), i, ContactsNameIndex);
            ContactsDataModel.setValueAt(record.getEmail(), i, ContactsEmailIndex);
            ContactsDataModel.setValueAt(record.getMobilePhone(), i, ContactsMobileIndex);
            ContactsDataModel.setValueAt(record.getHomePhone(), i, ContactsHomeNumberIndex);
            i++;//Go to next Line in the table
        }
    }

    private void fillMemosTable() {
        MemoTablePrepair();
        jTabbedPane1.setTitleAt(MemostabINDEX, "Memos (" + totalMemos + ")");
        int i = 0;

        for (Memo record : database.memos()) {

            MemosDataModel.setValueAt(record.getTitle(), i, MemosTitleIndex);
            MemosDataModel.setValueAt(record.getMemo(), i, MemosMemoIndex);
            i++;//Go to next Line in the table
        }
    }

    private void jTableSMSMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSMSMouseClicked
        tableClick(evt);
}//GEN-LAST:event_jTableSMSMouseClicked

    private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
        SMSSelectedRows = jTableSMS.getSelectedRows();
        ContactsSelectedRows = jTableContacts.getSelectedRows();
        MemosSelectedRows = jTableMemos.getSelectedRows();
        ActiveTAB = jTabbedPane1.getSelectedIndex();


        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            if (saveDialog()) {
                if (ext.equalsIgnoreCase("txt")) {
                    fileWriter.writeTextToFile(fToSave, smsWriter.SMSToPlainText(SMSSelectedRows), ".txt");
                } else if (ext.equalsIgnoreCase("csv")) {
                    fileWriter.writeTextToFile(fToSave, smsWriter.SMSToCVS(SMSSelectedRows), ".csv");
                } else if (ext.equalsIgnoreCase("xml")) {
                    fileWriter.writeXMLtoFile(fToSave, smsWriter.SMSToXML(SMSSelectedRows));
                }
            }
        } else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            if (saveDialog()) {
                if (ext.equalsIgnoreCase("txt")) {
                    fileWriter.writeTextToFile(fToSave, ContactsWriter.ContactsToPlainText(ContactsSelectedRows), ".txt");
                } else if (ext.equalsIgnoreCase("csv")) {
                    fileWriter.writeTextToFile(fToSave, ContactsWriter.ContactsToCVS(ContactsSelectedRows), ".csv");
                } else if (ext.equalsIgnoreCase("xml")) {
                    fileWriter.writeXMLtoFile(fToSave, ContactsWriter.ContactsToXML(ContactsSelectedRows));
                }
            }
        } else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            if (saveDialog()) {
                if (ext.equalsIgnoreCase("txt")) {
                    fileWriter.writeTextToFile(fToSave, MemosWriter.MemosToPlainText(MemosSelectedRows), ".txt");
                } else if (ext.equalsIgnoreCase("csv")) {
                    fileWriter.writeTextToFile(fToSave, MemosWriter.MemosToCVS(MemosSelectedRows), ".csv");
                } else if (ext.equalsIgnoreCase("xml")) {
                    fileWriter.writeXMLtoFile(fToSave, MemosWriter.MemosToXML(MemosSelectedRows));
                }
            }
        } else {
            JOptionPane.showMessageDialog(MessageFrame, "Select the items you want to save");
        }


    }//GEN-LAST:event_saveAsMenuItemActionPerformed
    private boolean saveDialog() {
        if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(null)) {

            int lastDot = jFileChooser1.getFileFilter().getDescription().lastIndexOf(".") + 1;
            ext = jFileChooser1.getFileFilter().getDescription().substring(lastDot);
            fToSave = jFileChooser1.getSelectedFile().getAbsolutePath() + "." + ext;
            //System.out.println(fToSave+" - "+ext);
            return true;
        }
        return false;
    }
    private void ShowPopup(MouseEvent e) {
        jPopupMenu.show(e.getComponent(),
                e.getX(), e.getY());
    }

    private void jMenuItemXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemXMLActionPerformed
        String tmp = "";
        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToXML(SMSSelectedRows).asXML();
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToXML(ContactsSelectedRows).asXML();
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToXML(MemosSelectedRows).asXML();
        }



        viewer.setXml(tmp);
        viewer.setTitle("SMS Viewer - XML");
        viewer.setVisible(true);
}//GEN-LAST:event_jMenuItemXMLActionPerformed

    private void jMenuItemTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemTxtActionPerformed

        String tmp = "";
        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToPlainText(SMSSelectedRows);
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToPlainText(ContactsSelectedRows);
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToPlainText(MemosSelectedRows);
        }


        viewer.setTxt(tmp);
        viewer.setTitle("SMS Viewer - Plain Text");
        viewer.setVisible(true);
}//GEN-LAST:event_jMenuItemTxtActionPerformed

    private void jMenuItemCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCSVActionPerformed
        String tmp = "";

        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToCVS(SMSSelectedRows);
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToCVS(ContactsSelectedRows);
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToCVS(MemosSelectedRows);
        }

        viewer.setCvs(tmp);
        viewer.setTitle("SMS Viewer - Csv");
        viewer.setVisible(true);
}//GEN-LAST:event_jMenuItemCSVActionPerformed

    private void jMenuItemCPTXTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCPTXTActionPerformed
        String tmp = "";
        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToPlainText(SMSSelectedRows);
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToPlainText(ContactsSelectedRows);
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToPlainText(MemosSelectedRows);
        }

        setClipboardContents(tmp);
}//GEN-LAST:event_jMenuItemCPTXTActionPerformed

    private void jMenuItemCPXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCPXMLActionPerformed
        String tmp = "";
        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToXML(SMSSelectedRows).asXML();
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToXML(ContactsSelectedRows).asXML();
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToXML(MemosSelectedRows).asXML();
        }
        setClipboardContents(tmp);
}//GEN-LAST:event_jMenuItemCPXMLActionPerformed

    private void jMenuItemCPCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCPCSVActionPerformed
        String tmp = "";

        if (ActiveTAB == SMStabINDEX && totalSMS != 0 && SMSSelectedRows.length > 0) {
            tmp = smsWriter.SMSToCVS(SMSSelectedRows);
        }
        else if (ActiveTAB == ContactstabINDEX && totalContacts != 0 && ContactsSelectedRows.length > 0) {
            tmp = ContactsWriter.ContactsToCVS(ContactsSelectedRows);
        }
        else if (ActiveTAB == MemostabINDEX && totalMemos != 0 && MemosSelectedRows.length > 0) {
            tmp = MemosWriter.MemosToCVS(MemosSelectedRows);
        }
        setClipboardContents(tmp);
}//GEN-LAST:event_jMenuItemCPCSVActionPerformed

    private void jTableContactsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableContactsMouseClicked
        tableClick(evt);
}//GEN-LAST:event_jTableContactsMouseClicked
    private void tableClick(java.awt.event.MouseEvent evt) {
        int[] SelectedRows = new int[0];
        String evtObj = null;
        ActiveTAB = jTabbedPane1.getSelectedIndex();

        if (ActiveTAB == SMStabINDEX) {
            SelectedRows = SMSSelectedRows = jTableSMS.getSelectedRows();
            evtObj = "SMS";
        } else if (ActiveTAB == ContactstabINDEX) {
            SelectedRows = ContactsSelectedRows = jTableContacts.getSelectedRows();
            evtObj = "Contacts";
        } else if (ActiveTAB == MemostabINDEX) {
            SelectedRows = MemosSelectedRows = jTableMemos.getSelectedRows();
            evtObj = "Memos";
        }


        if (evt.getButton() == MouseEvent.BUTTON3 && SelectedRows.length > 0) {
            ShowPopup(evt);

        } else {
            if (evt.getButton() == MouseEvent.BUTTON3 && SelectedRows.length == 0) {
                JOptionPane.showMessageDialog(MessageFrame, "Select the " + evtObj + " you want to View");
            }
        }
        if (evt.getClickCount() == 2) {
            //System.out.println("double click");
        }

    }
    private void ResolveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResolveCheckBoxActionPerformed
        resolveNames = ResolveCheckBox.isSelected();

        if (saveAsMenuItem.isEnabled()) {
            smsWriter = new SmsWriters(database, resolveNames);
            fillSMSTable();
        }
    }//GEN-LAST:event_ResolveCheckBoxActionPerformed

    private void jTableMemosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMemosMouseClicked
        tableClick(evt);
}//GEN-LAST:event_jTableMemosMouseClicked

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new IpdDump_NewGUI().setVisible(true);
//            }
//        });
//    }
    // <editor-fold defaultstate="collapsed" desc="Table Prepair Code - - NEED TO find alternative way of doing this??">
    private void MemoTablePrepair() {
        jTableMemos.setModel(new javax.swing.table.DefaultTableModel(
                new Object[totalMemos][1],
                new String[]{
                    "Title", "Memo"
                }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTableMemos);
        jTableMemos.getColumnModel().getColumn(0).setPreferredWidth(150);
        jTableMemos.getColumnModel().getColumn(0).setMaxWidth(150);

        javax.swing.GroupLayout jPanelMemoLayout = new javax.swing.GroupLayout(jPanelMemo);
        jPanelMemo.setLayout(jPanelMemoLayout);
        jPanelMemoLayout.setHorizontalGroup(
                jPanelMemoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE));
        jPanelMemoLayout.setVerticalGroup(
                jPanelMemoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanelMemoLayout.createSequentialGroup().addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        MemosDataModel = jTableMemos.getModel();
    }

    private void ContactsTablePrepair() {
        jTableContacts.setModel(new javax.swing.table.DefaultTableModel(
                new Object[totalContacts][3], new String[]{
                    "Name", "Email", "Mobile", "Home"
                }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jTableContacts.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableContactsMouseClicked(evt);
            }
        });

        ContactsDataModel = jTableContacts.getModel();
    }

    private void smsTablePrepair() {
        jTableSMS.setModel(new javax.swing.table.DefaultTableModel(
                new Object[totalSMS][4],
                new String[]{
                    "Sent?", "Number", "Text", "Sent Date", "Received Date"
                }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });

        jScrollPane1.setViewportView(jTableSMS);
        jTableSMS.getColumnModel().getColumn(0).setMinWidth(40);
        jTableSMS.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTableSMS.getColumnModel().getColumn(0).setMaxWidth(40);
        jTableSMS.getColumnModel().getColumn(1).setMaxWidth(130);
        jTableSMS.getColumnModel().getColumn(2).setMinWidth(150);
        jTableSMS.getColumnModel().getColumn(3).setMaxWidth(200);
        jTableSMS.getColumnModel().getColumn(4).setMaxWidth(200);

        javax.swing.GroupLayout jPanelSMSLayout = new javax.swing.GroupLayout(jPanelSMS);
        jPanelSMS.setLayout(jPanelSMSLayout);
        jPanelSMSLayout.setHorizontalGroup(
                jPanelSMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE));
        jPanelSMSLayout.setVerticalGroup(
                jPanelSMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE));

        SMSDataModel = jTableSMS.getModel();
    }// </editor-fold>

    public static void setClipboardContents(String aString) {
        StringSelection stringSelection = new StringSelection(aString);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, (ClipboardOwner) null);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser IpdChooser;
    private javax.swing.JFrame MessageFrame;
    private javax.swing.JCheckBoxMenuItem ResolveCheckBox;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JMenu jMenu;
    private javax.swing.JMenuItem jMenuItemCPCSV;
    private javax.swing.JMenuItem jMenuItemCPTXT;
    private javax.swing.JMenuItem jMenuItemCPXML;
    private javax.swing.JMenuItem jMenuItemCSV;
    private javax.swing.JMenuItem jMenuItemTxt;
    private javax.swing.JMenuItem jMenuItemXML;
    private javax.swing.JPanel jPanelCalendar;
    private javax.swing.JPanel jPanelContacts;
    private javax.swing.JPanel jPanelMemo;
    private javax.swing.JPanel jPanelOptions;
    private javax.swing.JPanel jPanelSMS;
    private javax.swing.JPanel jPanelTasks;
    private javax.swing.JPopupMenu jPopupMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableContacts;
    private javax.swing.JTable jTableMemos;
    private javax.swing.JTable jTableSMS;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JLabel status_label;
    private javax.swing.JLabel status_label5;
    private javax.swing.JLabel status_label6;
    private javax.swing.JLabel status_label7;
    // End of variables declaration//GEN-END:variables
}
